package cn.hill4j.tool.spring.ext.mvc;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
  * ClassName: WrapMethodArgumentResolver
  * Description: 请求参数解析包装执行器
  * @author hillchen
  * Date: 2023/8/13
  * @version: 1.0
  */
public class WrapMethodArgumentResolver implements HandlerMethodArgumentResolver {
    private final HandlerMethodArgumentResolver targetResolver ;
    /**
     * @author hillchen
     * Description: 构造方法
     * Date: 2023/8/13 00:48
     * @param targetResolver 原执行器
     * @return void
     **/
    private WrapMethodArgumentResolver(HandlerMethodArgumentResolver targetResolver) {
        this.targetResolver = targetResolver;
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return targetResolver.supportsParameter(parameter);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        Object arg = targetResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        return arg;
    }
    /**
     * @author hillchen
     * Description: 包装执行器
     * Date: 2023/8/13 00:48
     * @param handlerMethodArgumentResolver  原执行器
     * @return HandlerMethodArgumentResolver
     **/
    public static HandlerMethodArgumentResolver wrap(HandlerMethodArgumentResolver handlerMethodArgumentResolver){
        return new WrapMethodArgumentResolver(handlerMethodArgumentResolver);
    }
}
